package com.tom.org.controller;

import com.tom.basic.utils.AjaxResult;
import com.tom.basic.utils.PageList;
import com.tom.org.domain.Department;
import com.tom.org.query.DepartmentQuery;
import com.tom.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(value = "部门的API",description="部门相关的CRUD功能")
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService DepartmentService;
    /**
    * @Title: 根据id查询
    * @Description: TODO
    * @Author: LT
    * @Version: 1.0
    * @Date:
    * @Parameters:
    * @Return
    */
    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public Department loadById(@ApiParam(value = "ID属性",required = true) @PathVariable("id")Long id){
       return DepartmentService.loadById(id);
   }
    /**
    * @Title: 查询所有
    * @Description: TODO
    * @Author: LT
    * @Version: 1.0
    * @Date:
    * @Parameters:
    * @Return
    */
   @GetMapping
   public List<Department> loadAll(){
       return DepartmentService.loadAll();
   }
   /** 
   * @Title: 高级查询
   * @Description: TODO
   * @Author: LT
   * @Version: 1.0
   * @Date:     
   * @Parameters: 
   * @Return 
   */ 
   @PostMapping
    public PageList<Department> queryData(@RequestBody DepartmentQuery departmentQuery) {
        return DepartmentService.queryData(departmentQuery);
    }
    /** 
    * @Title: 根据id删除
    * @Description: TODO
    * @Author: LT
    * @Version: 1.0
    * @Date:     
    * @Parameters: 
    * @Return 
    */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id")Long id){
       try {
           DepartmentService.remove(id);
           return AjaxResult.me();
       } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage("系统繁忙,请稍后重试");
       }
    }
    /** 
    * @Title: 增加和删除
    * @Description: TODO
    * @Author: LT
    * @Version: 1.0
    * @Date:     
    * @Parameters: 
    * @Return 
    */
    @PutMapping
    public AjaxResult updateOrAdd(@RequestBody Department department) {
        try {
            if (department.getId() == null){
                DepartmentService.save(department);
            } else {
                DepartmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage("系统繁忙,请稍后重试");
        }
    }
}
